/**
 * Copyright (C), 2002-2017, 上海巧房信息技术有限公司
 * FileName: ElementRuleFacadeImpl.java
 * Author:   yi.li
 * Date:     2018年6月29日 下午2:43:07
 * Description:  采集规则
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.qiaofang.collectmanage.facade.impl;


import com.qiaofang.collectmanage.base.configuration.CollectManageConfiguration;
import com.qiaofang.collectmanage.service.ElementRuleService;
import com.qiaofang.collectmanage.stub.bean.ElementRuleDTO;
import com.qiaofang.collectmanage.stub.bean.GetFieldsDTO;
import com.qiaofang.collectmanage.stub.bean.SearchElementRulePageDTO;
import com.qiaofang.collectmanage.stub.constants.CollectRuleResponseCode;
import com.qiaofang.collectmanage.stub.constants.ElementRuleResponseCode;
import com.qiaofang.collectmanage.stub.request.*;
import com.qiaofang.collectmanage.stub.response.*;
import com.qiaofang.collectmanage.stub.request.SearchElementRulePageRequest;
import com.qiaofang.collectmanage.stub.facade.ElementRuleFacade;
import  com.qiaofang.collectmanage.stub.request.ListElementRuleRequest;
import com.qiaofang.collectmanage.stub.request.GetElementRuleRequest;
import com.qiaofang.common.constant.CommonResponseCode;
import com.qiaofang.common.response.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
public class ElementRuleFacadeImpl implements ElementRuleFacade {

	@Autowired
	private ElementRuleService elementRuleService;

	@Autowired
	private CollectManageConfiguration collectManageConfiguration;

	@Override
	public GetElementRuleByIdResponse getElementRuleById(@RequestBody @Valid GetElementRuleByIdRequest request) {
	   GetElementRuleByIdResponse response = new GetElementRuleByIdResponse();
		ElementRuleDTO dto = elementRuleService.getElementRuleById(request.getId());
		response.setData(dto);
		response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
		response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
		return response;
	}

	@Override
	public CommonResponse saveElementRule(@RequestBody @Valid SaveElementRuleRequest request) {
		elementRuleService.saveElementRule(request.getSaveElementRuleDTO());
		return new CommonResponse();
	}

	@Override
	public CommonResponse updateElementRule(@RequestBody @Valid UpdateElementRuleRequest request) {
		elementRuleService.updateElementRule(request.getUpdateElementRuleDTO());
		return new CommonResponse();
	}

	@Override
	public CommonResponse removeElementRule(@RequestBody @Valid RemoveElementRuleRequest request) {
		elementRuleService.removeElementRule(request.getRemoveElementRuleDTO());
		return new CommonResponse();
	}

	@Override
	public ListElementRuleResponse listElementRule(@RequestBody @Valid  ListElementRuleRequest request) {
		ListElementRuleResponse response = new ListElementRuleResponse();
		List<ElementRuleDTO> data = elementRuleService.listElementRule(request.getElementRuleDTO());
		response.setData(data);
		response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
		response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
		return response;
	}


	@Override
	public GetElementRuleResponse getElementRule(@RequestBody @Valid GetElementRuleRequest request) {
		GetElementRuleResponse response = new GetElementRuleResponse();
		ElementRuleDTO elementRuleDTO  = elementRuleService.getElementRule(request.getGetElementRuleDTO());
		response.setData(elementRuleDTO);
		return response;
	}


	@Override
	public SearchElementRulePageResponse searchElementRulePage(@RequestBody @Valid SearchElementRulePageRequest request) {
		SearchElementRulePageResponse response = new SearchElementRulePageResponse();
		SearchElementRulePageDTO searchElementRulePageDTO = new SearchElementRulePageDTO();
		List<ElementRuleDTO> list = elementRuleService.searchElementRulePage(request.getSearchElementRulePostDTO(),request.getPageDTO());
		searchElementRulePageDTO.setList(list);
		searchElementRulePageDTO.setCount(elementRuleService.countSearchElementRulePage(request.getSearchElementRulePostDTO()));
		response.setSearchElementRulePageDTO(searchElementRulePageDTO);
		response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
		response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
		return response;
	}


	@Override
	public TestFieldsByIdResponse testFieldsById(@RequestBody @Valid TestFieldsByIdRequest request) {
		TestFieldsByIdResponse testFieldsByIdResponse = new TestFieldsByIdResponse();
		testFieldsByIdResponse.setData(elementRuleService.testFieldsById(request.getIds()));
		testFieldsByIdResponse.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
		testFieldsByIdResponse.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
		return testFieldsByIdResponse;
	}

	@Override
	public TestFieldsByIdResponse testToolFieldsById(@RequestBody @Valid TestToolFieldsByIdRequest request) {
		TestFieldsByIdResponse testFieldsByIdResponse = new TestFieldsByIdResponse();
		testFieldsByIdResponse.setData(elementRuleService.testToolFieldsById(request.getIds(),request.getSrcTitleLink()));
		testFieldsByIdResponse.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
		testFieldsByIdResponse.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
		return testFieldsByIdResponse;
	}


	@Override
	public GetFieldsResponse getFields(@RequestBody @Valid GetFieldsRequest request) {
		GetFieldsResponse getFieldsResponse = new GetFieldsResponse();
		List<String> data = elementRuleService.getFields(request.getCollectRuleUuid());
		List<String> existData = elementRuleService.getExistFields(request.getCollectRuleUuid());
		GetFieldsDTO getFieldsDTO = new GetFieldsDTO();
		getFieldsDTO.setData(data);
		getFieldsDTO.setExistData(existData);
		getFieldsResponse.setGetFieldsDTO(getFieldsDTO);
		return getFieldsResponse;
	}
}
